package org.example.read;

import org.example.pojo.BOM;
import org.example.pojo.Member;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 读动态域文件
 */
public class ReadDomains {
    private static final String SHARP = "#";


    /**
     * 读取:动态域_zh_CN.voc文件
     * 此方法可以实例化动态域的BOM到XML列
     *
     * @param fileName 文件路径
     */
    public static List<BOM> readzhCNFile(String fileName) {
        File file = new File(fileName);
        List<BOM> bomList = null;
        if (!file.exists()) {
            System.out.println("文件不存在!" + fileName);
            return null;
        }
        BufferedReader bufferedReader = null;
        FileReader fileReader = null;
        try {
            fileReader = new FileReader(file);
            bufferedReader = new BufferedReader(fileReader);
            String line;
            bomList = new ArrayList<>();
            BOM bom = new BOM();
            String className = "";
            while ((line = bufferedReader.readLine()) != null) {
                if (line.startsWith(SHARP)) {
                    if (!"".equals(className)) {
                        bomList.add(bom);
                        bom = new BOM();
                    }
                    className = line.split(SHARP)[1].trim();
                    bom.setClassName(className);
                }
                if (line.startsWith(className)) {
                    //com.sinosoft.brms.nb.uw.ind.agent.AgentCate#concept.label = 代理人的分类
                    if (line.contains("concept.label")) {
                        String description = line.split("=")[1].trim();
                        bom.setDescription(description);
                    }
                    //com.sinosoft.brms.nb.uw.ind.agent.AgentCate.code0#instance.label = 0_所有系列
                    if (line.contains("instance.label")) {
                        //处理label列
                        String label = line.split("=")[1].trim();
                        String temp = line.split("#")[0].trim();
                        int startIndex = temp.lastIndexOf(".") + 1;
                        String code = temp.substring(startIndex);
                        Member member = new Member();
                        member.setLabel(label);
                        member.setCode(code);
                        bom.addMembers(member);
                        System.out.println(member);
                    }
                }
            }
            //最后一个动态域
            if (!"".equals(className)) {
                bomList.add(bom);
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            closeFileReader(fileReader);
            closeBufferedReader(bufferedReader);

        }
        return bomList;
    }


    /**
     * 读取:动态域_zh_CN.voc文件
     * 此方法可以实例化动态域的BOM到XML列
     *
     * @param fileName 文件路径
     */
    public static List<BOM> readzhCNFile2(String fileName) {
        File file = new File(fileName);
        List<BOM> bomList = null;
        if (!file.exists()) {
            System.out.println("文件不存在!" + fileName);
            return null;
        }
        BufferedReader bufferedReader = null;
        FileReader fileReader = null;
        try {
            fileReader = new FileReader(file);
            bufferedReader = new BufferedReader(fileReader);
            String line;
            bomList = new ArrayList<>();
            BOM bom = new BOM();
            String className = "";
            while ((line = bufferedReader.readLine()) != null) {
                if (line.startsWith(SHARP)) {
                    if (!"".equals(className)) {
                        bomList.add(bom);
                        bom = new BOM();
                    }
                    className = line.split(SHARP)[1].trim();
                    bom.setClassName(className);
                }
                if (line.startsWith(className)) {
                    //AgentChannel#concept.label = 销售渠道
                    if (line.contains("concept.label")) {
                        String description = line.split("=")[1].trim();
                        bom.setDescription(description);
                    }
                    //AgentChannel.code1#instance.label = 1_个险代理人
                    if (line.contains("instance.label")) {
                        String label = line.split("=")[1].trim();
                        int startIndex = line.indexOf(".") + 1;
                        int endIndex = line.indexOf("#");
                        String code = line.substring(startIndex, endIndex);
                        Member member = new Member();
                        member.setLabel(label);
                        member.setCode(code);
                        bom.addMembers(member);
                    }
                }
            }
            //最后一个动态域
            if (!"".equals(className)) {
                bomList.add(bom);
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            closeFileReader(fileReader);
            closeBufferedReader(bufferedReader);

        }
        return bomList;
    }

    public static void closeFileReader(FileReader fileReader) {
        if (fileReader != null) {
            try {
                fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void closeBufferedReader(BufferedReader bufferedReader) {
        if (bufferedReader != null) {
            try {
                bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
